package com.example.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Label;
import com.example.book.entity.Type;
import com.example.book.mapper.LabelMapper;
import com.example.book.service.ILabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements ILabelService {

    @Autowired
    private LabelMapper labelMapper;

    /**
     * 分页查询标签
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public ResponseResult pageInfo(Integer page, Integer pageSize, String name) {
        Page<Label> page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Label> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Label::getName, name);
        queryWrapper.eq(Label::getIsDelete,1);
        Page<Label> labelPage = this.page(page1, queryWrapper);
        return ResponseResult.okResult(labelPage);
    }

    /**
     * 添加榜单信息
     *
     * @param label
     * @return
     */
    @Override
    public ResponseResult addLabel(Label label) {
        labelMapper.insert(label);
        return ResponseResult.okResult(label) ;
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult label(Integer id) {
        Label label = labelMapper.selectById(id);
        return ResponseResult.okResult(label);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteLabel(Long[] ids) {
        for (Long id : ids) {
            Label label = this.getById(id);
            Boolean status = label.getStatus();
            if (status){
                return ResponseResult.errorResult(500,"榜单类型状态正常不能删除");
            }
            LambdaUpdateWrapper<Label> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Label::getId, id);
            updateWrapper.set(Label::getIsDelete, 0);
            this.update(updateWrapper);
        }
        return ResponseResult.okResult("榜单类型删除成功");
    }

    /**
     * 修改图书状态
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateStatus(Long id) {
        Label label = labelMapper.selectById(id);
        if (!label.getStatus()) {
            label.setStatus(true);
        } else {
            label.setStatus(false);
        }
        labelMapper.updateById(label);
        return ResponseResult.okResult(label);
    }
}
